package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.VO.OrderBaseWithDetailsVO;
import com.chixing.entity.OrderBase;
import com.chixing.entity.OrderDetail;
import com.chixing.entity.Transaction;
import com.chixing.entity.UserCoupon;
import com.chixing.mapper.OrderBaseMapper;
import com.chixing.service.IOrderBaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.service.IOrderDetailService;
import com.chixing.service.IUserCouponService;
import com.chixing.util.Result;
import com.chixing.util.TicketCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 订单基础信息服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-10-01
 */
@Service
public class OrderBaseServiceImpl extends ServiceImpl<OrderBaseMapper, OrderBase> implements IOrderBaseService {
    @Autowired
    private IUserCouponService userCouponService;
    @Autowired
    private IOrderDetailService orderDetailService;

    @Override
    public List<OrderBase> getAllOrders() {
        return baseMapper.selectList(null);
    }

    @Override
    public OrderBase getOrderById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrderWithDetails(OrderBase orderBase, List<OrderDetail> orderDetails) {
        // 初始化版本号
        orderBase.setVersion(0);
        // 生成取票码
        orderBase.setTicketCode(TicketCodeUtils.generateTicketCode());
        // 1. 保存订单基础信息
        boolean orderSaved = save(orderBase);


        if (!orderSaved) {
            throw new RuntimeException("订单保存失败");
        }

        // 2. 保存订单详情信息
        if (orderDetails != null && !orderDetails.isEmpty()) {
            for (OrderDetail detail : orderDetails) {
                detail.setOrderId(orderBase.getOrderId()); // 关联订单ID
                detail.setCreateTime(LocalDateTime.now());
                detail.setUpdateTime(LocalDateTime.now());
                detail.setVersion(0);

                boolean detailSaved = orderDetailService.saveOrderDetail(detail);
                if (!detailSaved) {
                    throw new RuntimeException("订单详情保存失败");
                }
            }
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeOrderWithDetails(Integer orderId) {
        // 1. 删除订单详情
        QueryWrapper<OrderDetail> detailQueryWrapper = new QueryWrapper<>();
        detailQueryWrapper.eq("order_id", orderId);
        boolean detailRemoved = orderDetailService.remove(detailQueryWrapper);

        // 2. 删除订单基础信息
        boolean orderRemoved = removeById(orderId);

        return detailRemoved && orderRemoved;
    }

    @Override
    public boolean updateOrder(OrderBase orderBase) {
        // 可添加更新时间等字段维护
        return updateById(orderBase);
    }

    @Override
    public List<OrderBaseWithDetailsVO> getOrdersByUserIdAndStatusWithDetails(Integer userId, Integer status) {
        // 查询订单基础信息
        QueryWrapper<OrderBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("statues", status);
        List<OrderBase> orderBases = list(queryWrapper);

        // 封装VO对象
        List<OrderBaseWithDetailsVO> result = new ArrayList<>();
        for (OrderBase orderBase : orderBases) {
            List<OrderDetail> orderDetails = orderDetailService.getDetailsByOrderId(orderBase.getOrderId());
            result.add(new OrderBaseWithDetailsVO(orderBase, orderDetails));
        }

        return result;
    }

    @Override
    public List<OrderBaseWithDetailsVO> getOrdersByUserIdAndAllStatusWithDetails(Integer userId) {
        // 查询订单基础信息
        QueryWrapper<OrderBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).in("statues", 1, 2, 3, 4);
        List<OrderBase> orderBases = list(queryWrapper);

        // 封装VO对象
        List<OrderBaseWithDetailsVO> result = new ArrayList<>();
        for (OrderBase orderBase : orderBases) {
            List<OrderDetail> orderDetails = orderDetailService.getDetailsByOrderId(orderBase.getOrderId());
            result.add(new OrderBaseWithDetailsVO(orderBase, orderDetails));
        }

        return result;
    }

}
